﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.SurveyManage
{
    public partial class SurveyItemsModify : Common.PageBase
    {
        T_SURVEY_BLL sBll = new T_SURVEY_BLL();
        T_SURVEY_ITEM_BLL iBll = new T_SURVEY_ITEM_BLL();
        T_SURVEY_ITEM_OPTION_BLL siopBll = new T_SURVEY_ITEM_OPTION_BLL();
        T_SURVEY_ITEMS_BLL isBll = new T_SURVEY_ITEMS_BLL();
        T_SURVEY_OPTIONS_BLL isopBll = new T_SURVEY_OPTIONS_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EnumHelper.FillListControl(ddlITEM_TYPE, typeof(ItemType));
                pageDataInit();
            }
        }

        #region 页面初始化数据绑定
        #endregion

        private void pageDataInit()
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();
            string _surveyid = String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]) ? "" : Request.QueryString["SURVEY_ID"].Trim();
            string _itemid = String.IsNullOrEmpty(Request.QueryString["ITEM_ID"]) ? "" : Request.QueryString["ITEM_ID"].Trim();
            if (!String.IsNullOrEmpty(_autoid))
            {
                T_SURVEY_ITEM_Entity entity = iBll.GetEntity("AUTOID", _autoid);
                PageHelper.SetControlValue<T_SURVEY_ITEM_Entity>(this.Page, entity);
                lblTitle.Text = "题目信息修改";
            }
            else if (!string.IsNullOrEmpty(_surveyid)&&!string.IsNullOrEmpty(_itemid))
            {
                T_SURVEY_ITEM_Entity entity = iBll.GetList(string.Format(" AND SURVEY_ID={0} AND ITEM_ID={1}", _surveyid, _itemid)).FirstOrDefault();
                PageHelper.SetControlValue<T_SURVEY_ITEM_Entity>(this.Page, entity);
                lblTitle.Text = "题目信息修改";
            }
            else
            {
                T_SURVEY_Entity entity = sBll.GetEntity("SURVEY_ID", _surveyid);
                if (entity != null)
                {
                    txtSURVEY_ID.Text = entity.SURVEY_ID.ToString();
                    txtSURVEY_NAME.Text = entity.SNAME;
                }
                string itemid = iBll.GetMaxValue("ITEM_ID", " AND SURVEY_ID='" + _surveyid + "'");
                int i = 1;
                if (!String.IsNullOrEmpty(itemid))
                {
                    i = StringHelper.ToInt(itemid) + 1;
                }
                txtITEM_ID.Text = i.ToString();
                txtITEM_NAME.Text = "";
                ddlITEM_TYPE.SelectedValue = ((int)ItemType.单选).ToString();
                rbENINPUT0.Checked = true;
                rbENNULL0.Checked = true;
                txtIINFO.Text = "";
                lblTitle.Text = "题目信息添加";
                this.btnSETITEM_OPTIONS.Visible = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtITEM_ID.Text == "")
            {
                PageHelper.ShowMessage(this.Page, "题目编号不能为空！");
                return;
            }
            if (!String.IsNullOrEmpty(txtSELECTITEMS.Text))
            {
                Save_from_Items();
                Response.Redirect(String.Format("SurveyItemsList.aspx?Survey_ID={0}&Survey_NAME={1}", txtSURVEY_ID.Text, txtSURVEY_NAME.Text));
                return;
            }
            if (txtITEM_NAME.Text == "")
            {
                PageHelper.ShowMessage(this.Page, "题目名称不能为空！");
                return;
            }
            List<T_SURVEY_ITEM_Entity> list = new List<T_SURVEY_ITEM_Entity>();
            T_SURVEY_ITEM_Entity entity = new T_SURVEY_ITEM_Entity();
            entity = iBll.GetList(string.Format(" AND ITEM_ID='{0}' AND SURVEY_ID='{1}'", txtITEM_ID.Text, txtSURVEY_ID.Text)).FirstOrDefault();
            if (entity == null)
            {
                entity = new T_SURVEY_ITEM_Entity();
                PageHelper.GetControlValue<T_SURVEY_ITEM_Entity>(this.Page, ref entity);
                entity.ITEM_TYPE = StringHelper.ToInt(ddlITEM_TYPE.SelectedValue);
                entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                iBll.Insert(entity);
            }
            else
            {
                PageHelper.GetControlValue<T_SURVEY_ITEM_Entity>(this.Page, ref entity);
                entity.ITEM_TYPE = StringHelper.ToInt(ddlITEM_TYPE.SelectedValue);
                iBll.Update(entity);
            }
            PageHelper.ShowMessage(this.Page, "保存成功！");
            Response.Redirect(String.Format("SurveyItemsList.aspx?Survey_ID={0}&Survey_NAME={1}", txtSURVEY_ID.Text,txtSURVEY_NAME.Text));
        }

        private void Save_from_Items()
        {
            string s_id = txtSURVEY_ID.Text;
            string s_name = txtSURVEY_NAME.Text;
            string i_id = txtITEM_ID.Text;
            string item_name = txtSELECTITEMS.Text;
            T_SURVEY_ITEMS_Entity isEntity = new T_SURVEY_ITEMS_Entity();
            isEntity = isBll.GetEntity("ITEM_NAME", item_name);
            if (isEntity == null)
            {
                PageHelper.ShowMessage(this.Page, "当前题库中不存在题目" + item_name + "，请从题库中选择题目！");
                return;
            }
            else
            {
                T_SURVEY_ITEM_Entity iEntity = new T_SURVEY_ITEM_Entity();
                long count = iBll.GetCount(" AND SURVEY_ID='" + s_id + "' and ITEM_NAME='" + item_name + "'");
                if (count > 0)
                {
                    PageHelper.ShowMessage(this.Page, "当前问卷中已存在题目" + item_name + "，请核实后再进行增加");
                    return;
                }
                iEntity = iBll.GetList(" AND SURVEY_ID='" + s_id + "' and ITEM_ID='" + i_id + "'").FirstOrDefault();
                if (iEntity == null)
                {
                    iEntity = new T_SURVEY_ITEM_Entity();
                    PageHelper.GetControlValue(this.Page, ref iEntity);
                    iEntity.SURVEY_ID = StringHelper.ToInt(s_id);
                    iEntity.ITEM_TYPE = isEntity.ITEM_TYPE;
                    iEntity.ENINPUT = isEntity.ENINPUT;
                    iEntity.ITEM_NAME = item_name;
                    iEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    iEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    iEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    iBll.Insert(iEntity);
                }
                else
                {
                    PageHelper.GetControlValue(this.Page, ref iEntity);
                    iEntity.SURVEY_ID = StringHelper.ToInt(s_id);
                    iEntity.ITEM_TYPE = isEntity.ITEM_TYPE;
                    iEntity.ENINPUT = isEntity.ENINPUT;
                    iEntity.ITEM_NAME = item_name;
                    iBll.Update(iEntity);
                }
                //将原来的题目选项清空
                T_SURVEY_ITEM_OPTION_Entity siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                siopBll.Delete(" AND ITEM_ID = " + i_id + " and SURVEY_ID = " + s_id);
                List<T_SURVEY_OPTIONS_Entity> lsisopEntity = new List<T_SURVEY_OPTIONS_Entity>();
                lsisopEntity = isopBll.GetList(" AND ITEM_ID='" + isEntity.AUTOID.ToString() + "'");
                foreach (T_SURVEY_OPTIONS_Entity isopEntity in lsisopEntity)
                {
                    siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                    siopEntity.OPTION_ID = isopEntity.OPTION_ID;
                    siopEntity.ITEM_ID = StringHelper.ToInt(i_id);
                    siopEntity.ITEM_NAME = item_name;
                    siopEntity.SURVEY_ID = StringHelper.ToInt(s_id);
                    siopEntity.SURVEY_NAME = s_name;
                    siopEntity.OPTION_NAME = isopEntity.OPTION_NAME;
                    siopEntity.NEXT_ITEM = 0;
                    siopEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    siopEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    siopEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    siopBll.Insert(siopEntity);
                }
            }
        }

        /// <summary>
        /// 设定题目选项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSETITEM_OPTIONS_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtSURVEY_ID.Text))
            {
                PageHelper.ShowMessage(this.Page, "问卷编号不能为空，请选择需要操作的问卷！");
                return;
            }
            if (string.IsNullOrEmpty(txtITEM_ID.Text))
            {
                PageHelper.ShowMessage(this.Page, "题目编号不能为空，请选择需要操作的问卷题目！");
                return;
            }
            if (String.IsNullOrEmpty(ddlITEM_TYPE.SelectedValue) || ddlITEM_TYPE.SelectedValue == ((int)ItemType.问答).ToString())
            {
                PageHelper.ShowMessage(this.Page, "题目类型为空或问答，不能做设定题目选项操作！");
                return;
            }
            T_SURVEY_ITEM_Entity entity = new T_SURVEY_ITEM_Entity();
            entity = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}'", txtSURVEY_ID.Text, txtITEM_ID.Text)).FirstOrDefault();
            if (entity == null)
            {
                PageHelper.ShowMessage(this.Page, "请先保存问卷题目，再做设定题目选项操作！");
                return;
            }
            Response.Redirect(String.Format("SurveyItemsOptionsList.aspx?SURVEY_ID={0}&SURVEY_NAME={1}&ITEM_ID={2}&ITEM_NAME={3}", txtSURVEY_ID.Text, txtSURVEY_NAME.Text,txtITEM_ID.Text,txtITEM_NAME.Text));
        }
    }
}
